import {
  fetchHomeGoodPriceData,
  fetchHomeHighScoreData,
  fetchHomeDiscountData,
  fetchHomeHotRecommendData,
  fetchHomeLongForData,
  fetchHomePlusData,
} from "@/apis/home";
import { createAsyncThunk, createSlice } from "@reduxjs/toolkit";

export const fetchHomeDataAction = createAsyncThunk("fetchData", async (payload, { dispatch }) => {
  fetchHomeGoodPriceData().then((res) => {
    dispatch(setGoodPrice(res));
  });
  fetchHomeHighScoreData().then((res) => {
    dispatch(setHighScore(res));
  });
  fetchHomeDiscountData().then((res) => {
    dispatch(setDiscount(res));
  });
  fetchHomeHotRecommendData().then((res) => {
    dispatch(setRecommend(res));
  });
  fetchHomeLongForData().then((res) => {
    dispatch(setLongFor(res));
  });
  fetchHomePlusData().then((res) => {
    dispatch(setPlusData(res));
  });
});

const homeSlice = createSlice({
  name: "home",
  initialState: {
    goodPriceInfo: {},
    highScoreInfo: {},
    discountInfo: {},
    recommendInfo: {},
    longForInfo: {},
    plusInfo: {},
  },
  reducers: {
    setGoodPrice(state, { payload }) {
      state.goodPriceInfo = payload;
    },
    setHighScore(state, { payload }) {
      state.highScoreInfo = payload;
    },
    setDiscount(state, { payload }) {
      state.discountInfo = payload;
    },
    setRecommend(state, { payload }) {
      state.recommendInfo = payload;
    },
    setLongFor(state, { payload }) {
      state.longForInfo = payload;
    },
    setPlusData(state, { payload }) {
      state.plusInfo = payload;
    },
  },
  // extraReducers: (builder) => {
  //   builder.addCase(fetchHomeDataAction.fulfilled, (state, { payload }) => {
  //     state.goodPriceInfo = payload;
  //   });
  // },
});

export const { setGoodPrice, setHighScore, setDiscount, setRecommend, setLongFor, setPlusData } = homeSlice.actions;
export default homeSlice.reducer;
